#!/bin/bash
#修改时间 2022-11-01
##############date_stamp=`date '+%Y%m%d%H%M%S'`
echo "test-dianshu  blockchain  service 2022-03-09"
branch_v=$2
user_id=$3
build_id=$4
status=$5
roll_num=$6
echo ${roll_num} ${status}
#jar地址
SERVCER_JAR=dianshu-blockchain-service-0.0.1-SNAPSHOT.jar
#日志名称
LOG_NAME=dianshu-blockchain-service.log
#进入对应目录
DIR="/home/ubuntu/blockchain-svc"
#输出信息
echo '---------------------------------------'
echo $DIR
UPLOAD_FOLDER_NAME=uploads
mkdir -p $UPLOAD_FOLDER_NAME
#创建项目jar包工程目录
if [ ! -d $DIR/jar ];then
  mkdir -p $DIR/jar
fi
#创建备份目录
if [ ! -d $DIR/bak/$build_id ];then
  mkdir -p $DIR/bak/$build_id
fi
date_stamp=`date "+%Y-%m-%d %H:%M:%S"`
date1=`date "+%Y-%m-%d-%H:%M:%S"`
echo $date_stamp
#备份
if [ ! -f "$DIR/jar/$SERVCER_JAR" ];then
  echo $SERVCER_JAR"文件不存在"
else
  cp $DIR/jar/$SERVCER_JAR  $DIR/bak/$build_id/
fi
#杀死java进程
_pid=`ps -ef | grep java | grep $SERVCER_JAR |awk '{print $2}'`
echo "kill ------>"$SERVCER_JAR "pid---->" $_pid
[ -n "$_pid" ] && kill -9 $_pid
#删除老包，替换新包
rm -rf $DIR/jar/$SERVCER_JAR
cp $DIR/$SERVCER_JAR  $DIR/jar
export JAVA_HOME=/usr/lib/jdk/jdk1.8.0_311
export JAVA=$JAVA_HOME/bin/java
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
#source /home/ubuntu/blockchain-svc/.env

echo '---------------------------------------java path'
#三十秒后打印日志
echo $DIR
echo "典枢Demo环境 blockchain  service jar  will be start!"
#############################################################################################
export GLOBAL_PASSWORD="$1" 
mkdir -p gclogs/
export GC_LOG_PATH=`pwd`/gclogs
####################  判断部署还是回滚 ##################
if [ "$status" = "Deploy" ]; then
  echo "开始正常部署"
  nohup java -jar \
    -Xms256m -Xmx256m -XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=128M -XX:+UseG1GC \
    -Xloggc:$GC_LOG_PATH/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps \
    -Dspring.profiles.active="test" \
    -Dserver.port=8180 \
    -Dspring.datasource.druid.stat-view-servlet.login-username="druid" \
    -Dspring.datasource.druid.stat-view-servlet.login-password="druid_dianshu" \
    -Dspring.datasource.url="jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&characterEncoding=utf-8" \
    -Dspring.datasource.username=dianshu \
    -Dspring.datasource.password='u%7mZqw)#tiy2tR' \
    -Dlogging.file.path="/home/ubuntu/dev/logs/dianshu-blockchain-service" \
    -Dinfura.network.host="yeezTest" \
    -Dinfura.project.id="26a8884164bc402fad4d783278f49ad8" \
    -Dyeez.rpc.host="62.234.122.91" \
    -Dyeez.rpc.port="8545" \
    -Dvirtual.wallet.password="asdfghjkl;'" \
    -Dupload.folder.path="/home/ubuntu/dev/dianshu-blockchain-service/uploads" \
    -Dsgx.sign.tool.path="/home/ubuntu/dev/dianshu-blockchain-service/uploads/sgx_sign" \
      $DIR/jar/$SERVCER_JAR   >>$DIR/logs/$LOG_NAME  2>&1     &
    sleep 5
    s_pid=`ps -ef | grep java | grep $SERVCER_JAR |awk '{print $2}'`
    send_weixin_v2  "【部署推送】\n【$date_stamp】\n【典枢】\n【blockchain/后端/区块链服务】\n【典枢Demo环境】【fenglin测试环境/43.138.115.87】\n【$branch_v】 分支部署完成, 进程pid：$s_pid ,由 $3 用户构建部署 "
  elif [ -d $DIR/bak/$roll_num  ] && [ -f $DIR/bak/$roll_num/$SERVCER_JAR ]; then
    echo "开始回滚操作"
    rm -rf $DIR/jar/$SERVCER_JAR;
    cp  $DIR/bak/$roll_num/$SERVCER_JAR  $DIR/jar/ ;
        echo " 拷贝回滚版本到启动目录！开始启动"
    nohup java -jar \
    -Xms256m -Xmx256m -XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=128M -XX:+UseG1GC \
    -Xloggc:$GC_LOG_PATH/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps \
    -Dspring.profiles.active="test" \
    -Dserver.port=8180 \
    -Dspring.datasource.druid.stat-view-servlet.login-username="druid" \
    -Dspring.datasource.druid.stat-view-servlet.login-password="druid_dianshu" \
    -Dspring.datasource.url="jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&characterEncoding=utf-8" \
    -Dspring.datasource.username=dianshu \
    -Dspring.datasource.password='u%7mZqw)#tiy2tR' \
    -Dlogging.file.path="/home/ubuntu/dev/logs/dianshu-blockchain-service" \
    -Dinfura.network.host="yeezTest" \
    -Dinfura.project.id="26a8884164bc402fad4d783278f49ad8" \
    -Dyeez.rpc.host="62.234.122.91" \
    -Dyeez.rpc.port="8545" \
    -Dvirtual.wallet.password="asdfghjkl;'" \
    -Dupload.folder.path="/home/ubuntu/dev/dianshu-blockchain-service/uploads" \
    -Dsgx.sign.tool.path="/home/ubuntu/dev/dianshu-blockchain-service/uploads/sgx_sign" \
      $DIR/jar/$SERVCER_JAR   >>$DIR/logs/$LOG_NAME  2>&1     &
    sleep 5
    s_pid=`ps -ef | grep java | grep $SERVCER_JAR |awk '{print $2}'`
    send_weixin_v2  "【回滚推送】\n【$date_stamp】\n【典枢】\n【blockchain/后端/区块链服务】\n【典枢Demo环境】【fenglin测试环境/43.138.115.87】\n【$branch_v】 分支部署完成,回滚版本为$roll_num 进程pid：$s_pid ,由 $3 用户构建部署 "
  fi
